package com.tduck.cloud.report.data.business.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.common.util.QueryWrapperUtils;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.report.data.business.entity.ReportDataSourceEntity;
import com.tduck.cloud.report.data.business.service.ReportDataSourceService;
import com.tduck.cloud.report.data.db.ReportDataConnectionFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tduck.cloud.common.validator.group.AddGroup;
import com.tduck.cloud.excel.util.ExcelUtils;


/**
 * 报数据源管理
 *
 * @author tduck
 * @since 2023-06-23 16:09:05
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/data/dataSource")
public class ReportDataSourceController {

    private final ReportDataSourceService reportDataSourceService;

    /**
     * 查询报数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:page')")
    @GetMapping("/page")
    public Result queryPage(Page page, ReportDataSourceEntity reportDataSource) {
        return Result.success(reportDataSourceService.page(page, QueryWrapperUtils.toSimpleQuery(reportDataSource)));
    }


    /**
     * 查询报数据源管理列表
     */
    @GetMapping("/list")
    public Result allList() {
        return Result.success(reportDataSourceService.list(Wrappers.<ReportDataSourceEntity>lambdaQuery().select(ReportDataSourceEntity::getId, ReportDataSourceEntity::getSourceName)));
    }


    /**
     * 导出报数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:export')")
    @GetMapping("/export")
    public void export(ReportDataSourceEntity reportDataSource) {
        List<ReportDataSourceEntity> list = reportDataSourceService.list(QueryWrapperUtils.toSimpleQuery(reportDataSource));
        ExcelUtils.exportExcel(list, "报数据源管理数据", ReportDataSourceEntity.class);
    }

    /**
     * 获取报数据源管理详细信息
     *
     * @param id 主键
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:query')")
    @GetMapping(value = "/{id}")
    public Result<ReportDataSourceEntity> getInfo(@PathVariable("id") Long id) {
        return Result.success(reportDataSourceService.getById(id));
    }

    /**
     * 新增报数据源管理
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:add')")
    @PostMapping("/add")
    public Result<Boolean> add(@Validated(AddGroup.class) @RequestBody ReportDataSourceEntity reportDataSource) {
        return Result.success(reportDataSourceService.save(reportDataSource));
    }

    /**
     * 修改报数据源管理
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:update')")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody ReportDataSourceEntity reportDataSource) {
        return Result.success(reportDataSourceService.updateById(reportDataSource));
    }

    /**
     * 删除报数据源管理
     *
     * @param ids 主键串
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:delete')")
    @PostMapping("/delete/{ids}")
    public Result<Boolean> delete(@PathVariable List<Long> ids) {
        return Result.success(reportDataSourceService.removeByIds(ids));
    }

    /**
     * 测试数据源
     */
    @PreAuthorize("@ss.hasPermi('data:dataSource:test')")
    @PostMapping(value = "testConnection")
    public Result<Boolean> testConnect(@RequestBody ReportDataSourceEntity sourceEntity) {
        if (null == sourceEntity) {
            return Result.failed("数据源不存在");

        }
        ReportDataConnectionFactory.testJdbcConnectionPool(sourceEntity.getSourceConfig());
        return Result.success();
    }
}
